package com.example.travel.controller;

import com.example.travel.pojo.ResultType;
import com.example.travel.pojo.orm.Note;
import com.example.travel.pojo.result.UploadResult;
import com.example.travel.pojo.frontend.FNote;
import com.example.travel.service.NoteService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.io.*;
import java.util.List;

@Slf4j
@RestController()
public class NoteController {

    @Autowired
    private NoteService noteService;

    @PostMapping("/note/upload_note")
    @ResponseBody
    public UploadResult uploadNote(FNote fNote, HttpServletRequest request) throws IOException {

        System.out.println("请求上传");
        System.out.println(fNote.toString());
        String serverPrefix = request.getScheme() + "://" + request.getServerName()
                + ":" + request.getServerPort()  +"/image/";

        boolean b = noteService.notePack(fNote, serverPrefix);

        if (b){
            return new UploadResult(ResultType.SUCCESS);
        }else {
            return new UploadResult(ResultType.FAIL);
        }
    }

    @PostMapping("/note/download_note")
    public List<Note> downloadNote(){
        return noteService.getAllNotes();
    }
}
